package com.cat.gridChart;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-number-of-fish-in-a-grid/description/
 * @create 2025/9/6 20:50
 * @since JDK17
 */

public class Solution05 {
    int n, m, ans;
    int dfs(int[][] grid, int x, int y) {
        if (x == -1 || x == n || y == -1 || y == m) {
            return 0;
        }
        if (grid[x][y] == 0) {
            return 0;
        }
        int ans = grid[x][y];
        grid[x][y] = 0;
        ans += dfs(grid, x - 1, y) + dfs(grid, x + 1, y) + dfs(grid, x, y + 1) + dfs(grid, x, y - 1);
        return ans;
    }

    public int findMaxFish(int[][] grid) {
        n = grid.length;
        m = grid[0].length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] != 0) {
                    ans = Math.max(ans, dfs(grid, i, j));
                }
            }
        }
        return ans;
    }
}
